

let WEEK_VALUES = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
//金额保留2位小数
Number.prototype.toMoney = function() {
	return this.toFixed(2);
}

String.prototype.dateToWeek = function() {
	return WEEK_VALUES[new Date(this).getDay()];
}
/**
 * 判断字符串中是否包含指定字符串
 */
String.prototype.contains = function(value) {
	return this.indexOf(value) != -1;
}
String.prototype.replaceAll = function(oldStr, newStr) {
    return this.replace(new RegExp(oldStr,"gm"), newStr);
}
/**
 * 判断数组是否包含指定元素
 * @param value
 * @returns {Boolean}
 */
Array.prototype.contains = function ( value ) {
  // for (var i in this) {
  //   if (this[i] == value) return true;
  // }
  // return false;
    return this.indexOf(value) != -1;
}
/**
 * json在数组中的索引
 * @param value json
 * @param key json 中的 key
 * @returns {number}
 */
Array.prototype.indexOfJson = function (value, key) {
	var k = key || 'id';
	for (var i = 0; i < this.length; i++) {
		if (this[i][k] == value[k]) {
			return i;
		}
	}
	return -1;
}
/**
 * 在数组中查找json对象，
 * @param key json 中的 key， 默认值为 id
 * @param value: key的值
 * @returns 数组中的json对象 或者 null
 */
Array.prototype.findJson = function(value, key) {
	var k = key || 'id';
	for (var i = 0; i < this.length; i++) {
		if (this[i][k] == value) {
			return this[i];
		}
	}
	return null;
}
/**
 * 清空数组
 */
Array.prototype.clear = function () {
    this.splice(0,this.length);
}
/**
 * 把字符串数组转换成json数组
 * @param key
 */
Array.prototype.toJsonArray = function (key) {
	key = key || 'id';
    var ms = [];
    this.forEach(function (v) {
    	var item = {};
    	item[key] = v;
        ms.push(item);
    });
    return ms;
}
/**
 * 加入数组
 * @param arr
 * @returns {Array}
 */
Array.prototype.pushAll = function (arr) {
	for (var i = 0; i < arr.length; i++) {
		this.push(arr[i]);
	}
	return this;
}
/**
 * 移除元素
 * @param index
 * @param length
 */
Array.prototype.remove = function (index, length) {
    this.splice(index, length || 1);
}
Array.prototype.removeValue = function (value) {
    this.splice(this.indexOf(value), 1);
}

/**
 * 数组格式化成字符串
 */
Array.prototype.toString = function(key, separator) {
	let type = typeof key;
	if (key == undefined) {
		var value = "";
		for (var i = 0; i < this.length; i++) {
			var v = this[i];
            value += v + ",";
        }
		return value.rtrim(",");
	} else if (type == "string") {
		var value = "";
        for (var i = 0; i < this.length; i++) {
            var v = this[i];
            value += (key ? v[key] : v) + (separator || ",");
        }
		return value.rtrim(separator || ",");
	} else if (type == "function") {
		var value = "";
        for (var i = 0; i < this.length; i++) {
            var v = this[i];
            value += key(i,v) + (separator || ",");
        }
		return value.rtrim(separator || ",");
	}
	return this;
}

/**
 * URL参数转json对象
 */
String.prototype.toJsonFromUrlParam = function() {
    var index = this.indexOf('?');
    var str = index == -1 ? this : this.substring(index + 1);
    var ret = {};
    str.split('&').forEach(val => {
        if (val.contains('=')) {
            index = val.indexOf('=');
            ret[val.substring(0, index)] = val.substring(index + 1);
        }
    });
    return ret;
}

/**
 * 删除左边字符
 * @param str
 * @returns
 */
String.prototype.ltrim = function(str){  
	var reg = new RegExp("^(" + str + ")*");
	return this.replace(reg, "");
}
/**
 * 删除右边字符
 * @param str
 * @returns
 */
String.prototype.rtrim = function(str){
	var reg = new RegExp("(" + str + ")*$");
	return this.replace(reg, "");
}
/**
 * 判断字符串是否是JSON格式
 * @returns
 */
String.prototype.isJSON = function() {
	return /^\{(.+:.+,*){1,}\}$/.test(this);
}
/**
 * 判断是否金额格式
 * @returns {boolean}
 */
String.prototype.isMoney = function () {
	return /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this);
}
/**
 * 判断字符串是否正整数
 * @returns
 */
String.prototype.isPlusInt = function() {
	//return /^[0-9]*[1-9][0-9]*$/.test(this);
	return /^[0-9]+$/.test(this);
}
/**
 * 判断是否已字符串str开头
 * @param str
 * @returns
 */
String.prototype.startWith=function(str){     
	  var reg=new RegExp("^"+str);     
	  return reg.test(this);        
	}  
/**
 * 判断是否已字符串str结尾
 * @param str
 * @returns
 */
String.prototype.endWith=function(str){     
  var reg=new RegExp(str+"$");     
  return reg.test(this);        
}
/**
 * 判断字符串是否符合日期格式
 * @returns
 */
String.prototype.isDate = function() {
	return /^(20)(\d{2})-(\d{2})-(\d{2})$/.test(this);
}
/**
 * 判断字符串是否符合时间格式，不含秒
 * 例：03:25
 * @returns
 */
String.prototype.isTime = function() {
	return /^(20|21|22|23|[0-1]?\d):[0-5]\d$/.test(this);
}
/**
 * 数字前面补0
 * @param length 总长度
 */
Number.prototype.prefixInt = function(length) {
	return (Array(length).join(0) + this).slice(-length);
}
/**
 * 分钟数加减
 * @param minute 往后加的分钟数，默认5分钟
 * @returns
 */
String.prototype.timeNext = function(minute) {
	if (minute == undefined) {
		minute = 5;
	}
	if (this.isTime()) {
		var timeArray = this.split(":");
		var totalMinute = (parseInt(timeArray[0]) * 60 + parseInt(timeArray[1]) + parseInt(minute)) % 1440;
		return parseInt(totalMinute / 60).prefixInt(2) + ":" + parseInt(totalMinute % 60).prefixInt(2);
	}
	return "";
}
/**
 * 日期天数加减
 * @param num 往前减的天数
 */
String.prototype.datePrev = function(num) {
	if (num == undefined) {
		num = 1;
	}
	if (this.isDate()) {
		var d = new Date(this);
		d.setDate(d.getDate() - num);
		return d.format();
	}
	return "";
}
/**
 * 日期天数加减
 * @param num 往后加的天数
 */
String.prototype.dateNext = function(num) {
	if (num == undefined) {
		num = 1;
	}
	if (this.isDate()) {
		var d = new Date(this);
		d.setDate(d.getDate() + num);
		return d.format();
	}
	return "";
}

/**
 * 取本周开始日期
 */
Date.prototype.weekStart = function() {
	var nowDayOfWeek = this.getDay(); //今天本周的第几天 
	var nowDay = this.getDate(); //当前日 
	var nowMonth = this.getMonth(); //当前月 
	var nowYear = this.getYear(); //当前年 
	nowYear += (nowYear < 2000) ? 1900 : 0; // 
	var weekStartDate = new Date(nowYear, nowMonth, nowDay - (nowDayOfWeek == 0 ? nowDayOfWeek + 7 : nowDayOfWeek) + 1);
	return weekStartDate.format();
}
/**
 * 取本月开始日期
 */
Date.prototype.monthStart = function() {
	this.setDate(1)
	return this.format();
}
/**
 * 取本月结束日期
 */
Date.prototype.monthEnd = function() {
	var year = this.getFullYear();
    var month = this.getMonth()+1;
    return new Date(year,month,0).format();
}
/**
 * 格式化日期
 * @returns
 */
Date.prototype.format = function() {
	var m = this.getMonth() + 1;
	var d = this.getDate();
	return this.getFullYear() + "-" + (m <= 9 ? "0" + m : m) + "-" + (d <= 9 ? "0" + d : d);
}

